home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / PHPUnit / TestSuite.php < prev   
PHP Script  |  2004-10-01  |  5KB  |  221 lines

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit                                                        |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
  7. // +------------------------------------------------------------------------+
  8. // | This source file is subject to version 3.00 of the PHP License,        |
  9. // | that is available at http://www.php.net/license/3_0.txt.               |
  10. // | If you did not receive a copy of the PHP license and are unable to     |
  11. // | obtain it through the world-wide-web, please send a note to            |
  12. // | license@php.net so we can mail you a copy immediately.                 |
  13. // +------------------------------------------------------------------------+
  14. //
  15. // $Id: TestSuite.php,v 1.11 2004/09/28 06:52:48 sebastian Exp $
  16. //
  17.  
  18. require_once 'PHPUnit/TestCase.php';
  19.  
  20. /**
  21.  * A TestSuite is a Composite of Tests. It runs a collection of test cases.
  22.  *
  23.  * Here is an example using the dynamic test definition.
  24.  *
  25.  * <code>
  26.  * <?php
  27.  * $suite = new PHPUnit_TestSuite();
  28.  * $suite->addTest(new MathTest('testPass'));
  29.  * ?>
  30.  * </code>
  31.  *
  32.  * Alternatively, a TestSuite can extract the tests to be run automatically.
  33.  * To do so you pass the classname of your TestCase class to the TestSuite
  34.  * constructor.
  35.  *
  36.  * <code>
  37.  * <?php
  38.  * $suite = new TestSuite('classname');
  39.  * ?>
  40.  * </code>
  41.  *
  42.  * This constructor creates a suite with all the methods starting with
  43.  * "test" that take no arguments.
  44.  *
  45.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  46.  * @copyright   Copyright © 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
  47.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  48.  * @category    PHP
  49.  * @package     PHPUnit
  50.  */
  51. class PHPUnit_TestSuite {
  52.     /**
  53.     * The name of the test suite.
  54.     *
  55.     * @var    string
  56.     * @access private
  57.     */
  58.     var $_name = '';
  59.  
  60.     /**
  61.     * The tests in the test suite.
  62.     *
  63.     * @var    array
  64.     * @access private
  65.     */
  66.     var $_tests = array();
  67.  
  68.     /**
  69.     * Constructs a TestSuite.
  70.     *
  71.     * @param  mixed
  72.     * @access public
  73.     */
  74.     function PHPUnit_TestSuite($test = FALSE) {
  75.         if ($test !== FALSE) {
  76.             $this->setName($test);
  77.             $this->addTestSuite($test);
  78.         }
  79.     }
  80.  
  81.     /**
  82.     * Adds a test to the suite.
  83.     *
  84.     * @param  object
  85.     * @access public
  86.     */
  87.     function addTest(&$test) {
  88.         $this->_tests[] = $test;
  89.     }
  90.  
  91.     /**
  92.     * Adds the tests from the given class to the suite.
  93.     *
  94.     * @param  string
  95.     * @access public
  96.     */
  97.     function addTestSuite($testClass) {
  98.         if (class_exists($testClass)) {
  99.             $methods       = get_class_methods($testClass);
  100.             $parentClasses = array(strtolower($testClass));
  101.             $parentClass   = $testClass;
  102.  
  103.             while(is_string($parentClass = get_parent_class($parentClass))) {
  104.                 $parentClasses[] = $parentClass;
  105.             }
  106.  
  107.             foreach ($methods as $method) {
  108.                 if (substr($method, 0, 4) == 'test' &&
  109.                     !in_array($method, $parentClasses)) {
  110.                     $this->addTest(new $testClass($method));
  111.                 }
  112.             }
  113.         }
  114.     }
  115.  
  116.     /**
  117.     * Counts the number of test cases that will be run by this test.
  118.     *
  119.     * @return integer
  120.     * @access public
  121.     */
  122.     function countTestCases() {
  123.         $count = 0;
  124.  
  125.         foreach ($this->_tests as $test) {
  126.             $count += $test->countTestCases();
  127.         }
  128.  
  129.         return $count;
  130.     }
  131.  
  132.     /**
  133.     * Returns the name of the suite.
  134.     *
  135.     * @return string
  136.     * @access public
  137.     */
  138.     function getName() {
  139.         return $this->_name;
  140.     }
  141.  
  142.     /**
  143.     * Runs the tests and collects their result in a TestResult.
  144.     *
  145.     * @param  object
  146.     * @access public
  147.     */
  148.     function run(&$result) {
  149.         for ($i = 0; $i < sizeof($this->_tests) && !$result->shouldStop(); $i++) {
  150.             $this->_tests[$i]->run($result);
  151.         }
  152.     }
  153.  
  154.     /**
  155.     * Runs a test.
  156.     *
  157.     * @param  object
  158.     * @param  object
  159.     * @access public
  160.     */
  161.     function runTest(&$test, &$result) {
  162.         $test->run($result);
  163.     }
  164.  
  165.     /**
  166.     * Sets the name of the suite.
  167.     *
  168.     * @param  string
  169.     * @access public
  170.     */
  171.     function setName($name) {
  172.         $this->_name = $name;
  173.     }
  174.  
  175.     /**
  176.     * Returns the test at the given index.
  177.     *
  178.     * @param  integer
  179.     * @return object
  180.     * @access public
  181.     */
  182.     function &testAt($index) {
  183.         if (isset($this->_tests[$index])) {
  184.             return $this->_tests[$index];
  185.         } else {
  186.             return FALSE;
  187.         }
  188.     }
  189.  
  190.     /**
  191.     * Returns the number of tests in this suite.
  192.     *
  193.     * @return integer
  194.     * @access public
  195.     */
  196.     function testCount() {
  197.         return sizeof($this->_tests);
  198.     }
  199.  
  200.     /**
  201.     * Returns the tests as an enumeration.
  202.     *
  203.     * @return array
  204.     * @access public
  205.     */
  206.     function &tests() {
  207.         return $this->_tests;
  208.     }
  209.  
  210.     /**
  211.     * Returns a string representation of the test suite.
  212.     *
  213.     * @return string
  214.     * @access public
  215.     */
  216.     function toString() {
  217.         return '';
  218.     }
  219. }
  220. ?>
  221.